package com.mybatis.test;

import com.mybatis.simple.mapper.UserMapper;
import com.mybatis.simple.pojo.TbUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 咕唧
 * @Date 2022/5/16 21:39
 * @Version 1.0
 */
public class DemoTest1 {

    @Test
    public void Test1() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        TbUser user = userMapper.findById(3L);
        System.out.println(user);
        sqlSession.close();
    }

    @Test
    public void Test2() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        Map map=new HashMap<>();
        map.put("username", "haha");
        map.put("password", "4297f44b13955235245b2497399d7a93");
        TbUser user = userMapper.findByNameAndPassword(map);
        System.err.println(user);
        sqlSession.close();
    }
    @Test
    public void Test3() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        TbUser user = userMapper.findWithNameAndPassword("haha", "4297f44b13955235245b2497399d7a93");
        System.err.println(user);
        sqlSession.close();
    }

    @Test
    public void Test4() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<TbUser> userList = userMapper.findByNameLike("ang");
        for (TbUser user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    @Test
    public void Test5() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        TbUser user=new TbUser();
        user.setUsername("haha");
        user.setPassword("4297f44b13955235245b2497399d7a93");
        TbUser tbUser = userMapper.findByNameAndPasswordWith(user);
        System.err.println(tbUser);
        sqlSession.close();
    }

    @Test
    public void Test6() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        TbUser user=new TbUser();
        user.setRole(0L);
//        user.setUsername("wangwu");
//        user.setPassword("e10adc3949ba59abbe56e057f20f883e");
        List<TbUser> users = userMapper.findByMulti(user);
        for (TbUser tbUser : users) {
            System.err.println(tbUser);
        }
        sqlSession.close();
    }
    @Test
    public void Test7() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<Long> ibs=new ArrayList<>();
        ibs.add(1L);
        ibs.add(2L);
        ibs.add(3L);
        ibs.add(4L);
        List<TbUser> userList = userMapper.findByIds(ibs);
        for (TbUser user : userList) {
            System.out.println(user);
        }

        sqlSession.close();
    }
}
